package net.zjitc.dao.Impl;

import net.zjitc.dao.CartDao;
import net.zjitc.pojo.CartItem;
import net.zjitc.pojo.Product;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/2.
 */
public class CartDaoImpl implements CartDao {
    QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());

    public List<CartItem> findByUid(int uid) {
        String sql = "SELECT * FROM product p,cartitem c WHERE p.pid=c.pid AND c.uid=? ORDER BY c.orderby";
        List<CartItem> cartItemList = new ArrayList<CartItem>();

        try {
            List<Map<String, Object>> list = runner.query(sql, new MapListHandler(), uid);
            for (Map<String, Object> m : list) {
                Product product = new Product();
                CartItem cartItem = new CartItem();
                BeanUtils.populate(product, m);
                BeanUtils.populate(cartItem, m);
                cartItem.setProduct(product);
                cartItemList.add(cartItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (cartItemList.size() > 0) {
            return cartItemList;
        }
        return null;
    }

    public CartItem findByPidAndUid(int pid, int uid) {
        String sql="select * from cartitem where pid=? and uid=?";

        try {
            CartItem cartItem= (CartItem) runner.query(sql,new BeanHandler(CartItem.class),pid,uid);
            return cartItem;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void add(CartItem cartItem) {
        String sql="insert into cartitem (cartid,pid,quantity,uid) values(?,?,?,?)";
        try {
            runner.update(sql,cartItem.getCartid(),cartItem.getProduct().getPid(),cartItem.getQuantity(),cartItem.getUser().getUid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void update(CartItem cartItem) {
        String sql="update cartitem set pid=?,quantity=?,uid=? where cartid=?";
        try {
            runner.update(sql,cartItem.getProduct().getPid(),cartItem.getQuantity(),cartItem.getUser().getUid(),cartItem.getCartid());
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public void removeCart(String pid) {
        String sql="delete from cartitem where pid=?";
        try {
            runner.update(sql,pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public void clearCart(int uid) {
        String sql="delete from cartitem where uid=?";
        try {
            runner.update(sql,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
